import yaml
from loguru import logger

import Config
from driver.driver import *


def chose_city(left, top, x, y, step=1):
    """
    选择相应的城池，供攻击，进入，派遣等方法使用
        1. 输入坐标
        2. 跳到坐标
        3. 点击城池，供下一步操作
    :param left: App的left坐标
    :param top: App的top坐标
    :param x: 城池的额x坐标
    :param y: 城池的y坐标
    :param step: 从第几步开始
    :return:
    """
    if step <= 1:
        # 1. 打开地图
        open_map(left, top)
    if step <= 2:
        # 2. 输入坐标
        _ = Config.DiTuConfig.XText
        mouse_move_to_and_click(left + _[0], top + _[1])
        clear_and_send_keys(str(x))
        _ = Config.DiTuConfig.YText
        mouse_move_to_and_click(left + _[0], top + _[1])
        clear_and_send_keys(str(y))
    if step <= 3:
        # 3. 跳到坐标
        pyautogui.hotkey("enter")
        time.sleep(2)
    if step <= 4:
        # 4. 点击城池，供下一步操作
        _ = Config.DiTuConfig.CenterCity
        mouse_move_to_and_click(left + _[0], top + _[1])


def entry_city(left, top, x, y, step=1, cityin=True):
    """
    进入城池
    :param left: App的left坐标
    :param top: App的top坐标
    :param x: 城池的x坐标
    :param y: 城池的y坐标
    :param step: 从第几步开始
    :param cityin: 是否进入城内
    :return:
    """
    chose_city(left, top, x, y, step=step)

    _ = Config.DiTuConfig.EntryButton
    mouse_move_to_and_click(left + _[0], top + _[1])
    if cityin:
        _ = Config.MainConfig.ChengNei
        mouse_move_to_and_click(left + _[0], top + _[1])


def send_commander(left, top, x, y):
    """
    选择城池进入派遣
    :param left: App的left坐标
    :param top: App的top坐标
    :param x: 城池的额x坐标
    :param y: 城池的y坐标
    :return:
    """
    open_map(left, top)
    chose_city(left, top, x, y)
    _ = Config.DiTuConfig.SendButton
    mouse_move_to_and_click(left + _[0], top + _[1])


def open_map(left, top):
    """
    打开大地图
    :param left:
    :param top:
    :return:
    """
    _ = Config.MainConfig.DiTu
    mouse_move_to_and_click(left + _[0], top + _[1])


def _read_config(control):
    control = control.GetParentControl().GetParentControl().GetParentControl().GetParentControl().GetParentControl().GetParentControl()
    _, server_name, username = control.Name.split("-")
    try:
        # 读取yaml文件内容
        with open("{}/config/{}.yml".format(Config.root_path, username), 'r', encoding="utf-8") as file:
            data = yaml.safe_load(file)
    except BaseException as e:
        logger.exception(e)
        logger.error("当前账户没有配置文件！")
        os._exit(1)
    try:
        server_config = data[server_name]
    except BaseException as e:
        logger.exception(e)
        logger.error("当前配置文件中不存在当前服务器配置！")
        os._exit(1)
    return server_config


def get_soldier_cities(control):
    """
    获取兵城坐标
    :param control:
    :return:
    """
    server_config = _read_config(control)
    return [_ for _ in server_config["soldierCities"].values()]


def get_special_soldier_cities(control):
    """
    获取特殊城池坐标——天降奇兵
    :param control:
    :return:
    """
    server_config = _read_config(control)
    if "specialSoldierCities" not in server_config.keys():
        return []
    return [_ for _ in server_config["specialSoldierCities"].values()]


def get_friendly_cities(control):
    """
    获取盟友城池坐标

    为运输功能使用
    :param control:
    :return:
    """
    server_config = _read_config(control)
    if "friendlyCities" not in server_config.keys():
        return []
    return [_ for _ in server_config["friendlyCities"].values()]


def get_main_city(control):
    """
    获取著称坐标
    :param control:
    :return:
    """
    server_config = _read_config(control)
    return server_config["mainCity"]


def get_all_city(control):
    """
    获取当前配置的所有城池
    :param control:
    :return:
    """
    server_config = _read_config(control)
    return [server_config["mainCity"]] + [_ for _ in server_config["soldierCities"].values()]


def get_sleep_time(control):
    """
    获取休眠时间

    征兵功能使用
    :param control:
    :return:
    """
    server_config = _read_config(control)
    return server_config["sleepTime"]


def get_translate_city(control):
    """
    获取运输城池坐标

    为运输功能使用
    :param control:
    :return:
    """
    server_config = _read_config(control)
    return server_config["TranslateCity"] if server_config["TranslateCity"] else None


def get_resource_points(control):
    """
    获取资源节点坐标
    :param control:
    :return:
    """
    server_config = _read_config(control)
    return server_config["resourcePositions"] if server_config["resourcePositions"] else []


def get_commander_city(control):
    """
    获取当前城池的城主城池坐标
    :param control:
    :return:
    """
    server_config = _read_config(control)
    if server_config["commanderCity"]:
        pass
    else:
        raise Exception("将领城池未配置，请配置")

    return server_config["commanderCity"]


def get_solider_cities_reserve_config(control):
    """
    获取当前兵城的反转配置

    为运输功能使用
    :param control:
    :return:
    """
    server_config = _read_config(control)
    return server_config["soldierCitiesReverse"] if server_config["soldierCitiesReverse"] else False


def get_people_num(control):
    """
    获取当前城池人口数
    :param control:
    :return:
    """
    control_left, control_top, _, _ = get_control_position(control)
    num = get_text_from_region(control_left + 130, control_top + 285, 65, 20)
    return int(num)


def get_food_num(control):
    """
    获取当前食物数量
    :param control:
    :return:
    """
    control_left, control_top, _, _ = get_control_position(control)
    # 点击资源按钮
    mouse_move_to_and_click(control_left + Config.ChengNeiConfig.Resource[0],
                            control_top + Config.ChengNeiConfig.Resource[1])

    num = get_text_from_region(control_left + 60, control_top + 355, 95, 25)
    return int(num)


def get_wood_num(control):
    """
    获取当前木材数量
    :param control:
    :return:
    """
    control_left, control_top, _, _ = get_control_position(control)
    # 点击资源按钮
    mouse_move_to_and_click(control_left + Config.ChengNeiConfig.Resource[0],
                            control_top + Config.ChengNeiConfig.Resource[1])

    num = get_text_from_region(control_left + 60, control_top + 385, 95, 25)
    return int(num)


def get_mine_num(control):
    """
    获取当前石料数量
    :param control:
    :return:
    """
    control_left, control_top, _, _ = get_control_position(control)
    # 点击资源按钮
    mouse_move_to_and_click(control_left + Config.ChengNeiConfig.Resource[0],
                            control_top + Config.ChengNeiConfig.Resource[1])

    num = get_text_from_region(control_left + 60, control_top + 415, 95, 25)
    return int(num)


def get_iron_num(control):
    """
    获取当前钢铁数量
    :param control:
    :return:
    """
    control_left, control_top, _, _ = get_control_position(control)
    # 点击资源按钮
    mouse_move_to_and_click(control_left + Config.ChengNeiConfig.Resource[0],
                            control_top + Config.ChengNeiConfig.Resource[1])

    num = get_text_from_region(control_left + 60, control_top + 445, 95, 25)
    return int(num)


def get_remaining_resource_num(control):
    """
    获取剩余资源数量
    :param control:
    :return:
    """
    control_left, control_top, _, _ = get_control_position(control)
    # 获取剩余资源
    num = get_text_from_region(control_left + 510, control_top + 470, 100, 20)
    return int(num)


if __name__ == "__main__":
    control = get_control_by_level([0, 0, 0, 0, 0, 0], base_name="热血三国-")
    # print(get_soldier_cities(control))
    # print(get_main_city(control))
    print(get_special_soldier_cities(control))
